package com.deer.wms.system.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.system.manage.dao.ReceivingAddressMapper;
import com.deer.wms.system.manage.model.address.ReceivingAddress;
import com.deer.wms.system.manage.model.address.ReceivingAddressCreate;
import com.deer.wms.system.manage.model.address.ReceivingAddressCriteria;
import com.deer.wms.system.manage.model.address.ReceivingAddressDto;
import com.deer.wms.system.manage.service.ReceivingAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by guo on 2019/11/13.
 */
@Service
@Transactional
public class ReceivingAddressServiceImpl extends ServiceImpl<ReceivingAddressMapper, ReceivingAddress> implements ReceivingAddressService {

    @Autowired
    private ReceivingAddressMapper receivingAddressMapper;

    @Autowired
    private ReceivingAddressService receivingAddressService;

    @Override
    public List<ReceivingAddressDto> findList(ReceivingAddressCriteria criteria) {
        return receivingAddressMapper.findList(criteria);
    }

    @Override
    public List<ReceivingAddressDto> findByCustomerId(Integer customerId){
        return receivingAddressMapper.findByCustomerId(customerId);
    }


    @Override
    public void createReceivingAddress(ReceivingAddressCreate create , CurrentUser currentUser){
        ReceivingAddress receivingAddress = new ReceivingAddress();
        BeanUtils.copyProperties(create, receivingAddress);

        if(create.getState() == 1){
            Integer customerId = create.getCustomerId();
            List<ReceivingAddressDto> list = receivingAddressService.findByCustomerId(customerId);
            for(ReceivingAddress receivingAddress1 : list){
                receivingAddress1.setState(0);
                super.updateById(receivingAddress1);
            }
        }
        super.save(receivingAddress);
    }

    @Override
    public ReceivingAddress findBy(String field, String value) {
        QueryWrapper<ReceivingAddress> qw = new QueryWrapper<>();
        return getOne(qw.eq(field, value));
    }

}
